﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace WeChatParking.Core.BLL
{
    public class Order_info
    {

        public Entity.Order_info GetOrder(string Guid)
        {
            if (string.IsNullOrWhiteSpace(Guid))
                return null;
            using (var dbContext = Repository.DbContext.Create())
            {
                string sql = String.Format("select * from  order_info where Guid='{0}'", Guid);

                var precidate = new { Guid = Guid };
                return dbContext.GetListBySql<Entity.Order_info>(sql, null).FirstOrDefault();
            }
        }

        /// <summary>
        /// 获取支付订单
        /// </summary>
        /// <param name="paged">是否分页</param>
        /// <param name="where">条件</param>
        /// <param name="filter">分页信息</param>
        /// <param name="summaryData"></param>
        /// <returns></returns>
        public Entity.PageList<Entity.OrderQuery> QueryOrderList(bool paged, string where, Entity.BaseFilter filter, out Entity.OrderQuery summaryData)
        {
            dynamic predicate = new System.Dynamic.ExpandoObject();
            if (paged == false)
            {
                filter.page = 1;
                filter.rows = Int32.MaxValue;
            }
            string dtSql = String.Format(@"SELECT o.order_id,o.Wxuser_name,o.car_no,o.total_fee,o.discount_fee,o.real_fee,o.pay_fee,o.pay_status,o.pay_type,o.pay_time,e1.create_time,o.EnterpriseID,e1.Enterprise_Name,o.CCEnterpriseID,e2.Enterprise_Name AS CCEnterprise_Name FROM dbo.order_info o
LEFT JOIN dbo.Enterprise e1 ON o.EnterpriseID=e1.ID
LEFT JOIN dbo.Enterprise e2 ON o.CCEnterpriseID=e2.ID {0}", where);
            dtSql += " ORDER BY o." + filter.sort + (filter.asc ? string.Empty : "  DESC");
            string sumSql = String.Format(@"SELECT  SUM(o.total_fee) AS total_fee,SUM(o.discount_fee) AS discount_fee,SUM(o.real_fee) AS real_fee,SUM(o.pay_fee)  AS pay_fee FROM dbo.order_info o  {0}", where);
            using (var dbContext = Repository.DbContext.Create())
            {
                var data = dbContext.GetListBySql<Entity.OrderQuery>(dtSql, predicate, filter.page - 1, filter.rows);
                summaryData = dbContext.GetListBySql<Entity.OrderQuery>(sumSql.ToString(), null).FirstOrDefault();
                return data;
            }
        }
        /// <summary>
        /// 获取到该车牌5分钟内无感支付状态是支付中的数据--然后发起查询（3次） 然后如果收到支付成功，在查询指令中判断是否是类型是4的数据有就发起退款
        /// </summary>
        /// <param name="card_no"></param>
        /// <returns></returns>
        public List<Entity.Order_info> getOrderList(string card_no)
        {
            string sql = String.Format("SELECT * FROM dbo.order_info WHERE car_no='{0}' AND pay_type=5 AND create_time>=DATEADD( minute,-5,GETDATE())", card_no);
            using (var dbContext = Repository.DbContext.Create())
            {
                var data = dbContext.GetListBySql<Entity.Order_info>(sql, null).ToList();
                return data;
            }

        }
        public List<Entity.Report.Order_Report> GetCurrentMeals(DateTime beginTime, DateTime endTime, int enID)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var ps = new Dapper.DynamicParameters();
                ps.Add("@beginTime", beginTime, DbType.DateTime, ParameterDirection.Input);
                ps.Add("@endTime", endTime, DbType.DateTime, ParameterDirection.Input);
                ps.Add("@enID", enID, DbType.Int32, ParameterDirection.Input);
                return dbContext.Execute<Entity.Report.Order_Report>("P_ORDER_Report", ps).ToList();
            }
        }

        public List<Entity.Report.SettlementReport> GetSettlementReport(int num, DateTime beginTime, DateTime endTime, int enID)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var ps = new Dapper.DynamicParameters();
                ps.Add("@Num", num, DbType.Int32, ParameterDirection.Input);
                ps.Add("@day", endTime, DbType.DateTime, ParameterDirection.Input);
                ps.Add("@beginTime", beginTime, DbType.String, ParameterDirection.Input);
                ps.Add("@enID", enID, DbType.Int32, ParameterDirection.Input);

                return dbContext.Execute<Entity.Report.SettlementReport>("P_SettlementReport", ps).ToList();
            }
        }
    }
}
